package com.briup.jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * JDBC封装工具类
 */
public class JdbcUtil {
    private static Properties properties;
    private static DataSource dataSource;
    static{
        properties = new Properties();
        try {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("com/briup/jdbc/db.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取连接对象（从pool里拿）
     */
    public static Connection getDruidConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    // 2、获取连接对象（自己创建）
    public static Connection getConnection(){
        String driver = properties.getProperty("driverClassName");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
            return connection;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    // 3、执行一些公共的DML和DDL
    public static int executeDmlOrDdl(String sql){
        Connection connection = getDruidConnection();
        Statement statement = null;
        int i = -1;
        try {
            statement = connection.createStatement();
            i = statement.executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(connection,statement);
        }
        return i;
    }
    // 4、执行一些简单的DQL得到结果集
    public static Map<String,Object> executeDql(String sql){
        Connection connection = getDruidConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        Map<String,Object> map = new HashMap<>();
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        map.put("connection",connection);
        map.put("statement",statement);
        map.put("resultSet",resultSet);
        return map;
    }
    // 5、提供关闭的方法
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void close(Connection connection, Statement statement){
        close(connection,statement,null);
    }
}
